﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Omnia.DataModels;
using Omnia.Enums;
using Omnia.Library.Extensions;
using System.Windows;

namespace Omnia.ViewModels
{
    public class StatisticsMonthlyViewModel : StatisticsViewModel
    {
        public StatisticsMonthlyViewModel()
        {
            if (IsDesignMode)
            { return; }
            LoadData();
        }

        private int year = DateTime.Now.Year;
        public int Year
        {
            get { return year; }
            set
            {
                year = value;
                RaisePropertyChanged(() => Year);
                LoadData();
                RaisePropertyChanged(() => StatIncomeCollection);
                RaisePropertyChanged(() => StatExpenseCollection);
                RaisePropertyChanged(() => MaxValue);
                RaisePropertyChanged(() => MajorStep);
                RaisePropertyChanged(() => HasData);
                RaisePropertyChanged(() => NodataIndicatorVisibility);
            }
        }

        public double MaxValue
        {
            get
            {
                double greaterNumber = 100;
                if (IsDesignMode)
                {
                    return 100;
                }
                double a = StatExpenseCollection != null ? StatExpenseCollection.Max(t => t.Amount) : 0;
                double b = StatIncomeCollection != null ? StatIncomeCollection.Max(t => t.Amount) : 0;
                greaterNumber = b;
                if (a > b)
                {
                    greaterNumber = a;
                }

                if (greaterNumber < 100)
                {
                    return 100;
                }
                long l = Convert.ToInt64(greaterNumber);
                int length = l.ToString().Length - 1;
                double subString = greaterNumber.ToString().Substring(1).ToDouble();
                Int64 subnumber = Convert.ToInt64(subString);

                Int64 addnumber = 0;

                if (subnumber != 0)
                {
                    Int64 _a = Convert.ToInt64("1".PadRight(length + 1, '0'));
                    if (_a / subnumber >= 2)
                    {
                        addnumber = _a / 2;
                    }
                    else
                    {
                        addnumber = _a;
                    }
                }


                double newNumber = Convert.ToDouble(greaterNumber.ToString().Substring(0, 1).PadRight(length + 1, '0'));//.ToDouble();

                return newNumber + addnumber;
            }
        }

        public double MajorStep
        {
            get { return MaxValue / 5; }
        }

        public bool HasData
        {
            get
            {
                double a = StatExpenseCollection != null ? StatExpenseCollection.Max(t => t.Amount) : 0;
                double b = StatIncomeCollection != null ? StatIncomeCollection.Max(t => t.Amount) : 0;

                return a + b > 0;
            }
        }

        public Visibility NodataIndicatorVisibility
        {
            get
            {
                return HasData ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        private ObservableCollection<TransactionStatisticInfo> statIncomeCollection;
        public ObservableCollection<TransactionStatisticInfo> StatIncomeCollection
        {
            get { return statIncomeCollection; }
            set
            {
                statIncomeCollection = value;
                RaisePropertyChanged(() => StatIncomeCollection);
            }
        }

        private ObservableCollection<TransactionStatisticInfo> statExpenseCollection;
        public ObservableCollection<TransactionStatisticInfo> StatExpenseCollection
        {
            get { return statExpenseCollection; }
            set
            {
                statExpenseCollection = value;
                RaisePropertyChanged(() => StatExpenseCollection);
            }
        }


        private void LoadData()
        {
            DateTime start = new DateTime(Year, 1, 1);
            DateTime end = new DateTime(Year + 1, 1, 1);
            IEnumerable<TransactionInfo> items = Database.CollectionOfTransactions.Where(t => t.Time >= start && t.Time < end);

            StatIncomeCollection = new ObservableCollection<TransactionStatisticInfo>(GetStatisticGroup(items.Where(t => t.Kind == TransactionKindValue.Income), StatisticKindValue.Monthly, 12));
            StatExpenseCollection = new ObservableCollection<TransactionStatisticInfo>(GetStatisticGroup(items.Where(t => t.Kind == TransactionKindValue.Expense), StatisticKindValue.Monthly, 12));
        }
    }
}
